﻿using System;
using System.Linq;
using MimosaProductsEF.Model;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace PDFGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document(PageSize.A4);

            PdfWriter.GetInstance(doc, new FileStream("..\\..\\salesreport.pdf", FileMode.Create));

            doc.Open();

            Font font = new Font(Font.FontFamily.HELVETICA, 10);
            Font fontBold = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD);

            PdfPTable table = new PdfPTable(5);
            table.WidthPercentage = 100;
            table.SetWidths(new float[] { 2,1,1,4,1 });

            Paragraph tempParagraph = new Paragraph("Aggregated Sales Report", fontBold);
            tempParagraph.Alignment = Element.ALIGN_CENTER;
            PdfPCell header = new PdfPCell();
            header.Colspan = 5;
            header.FixedHeight = 30f;
            header.HorizontalAlignment = Element.ALIGN_MIDDLE;
            header.AddElement(tempParagraph);

            table.AddCell(header);

            tempParagraph = new Paragraph("Product", fontBold);
            PdfPCell productCell = new PdfPCell();
            productCell.AddElement(tempParagraph);
            productCell.BackgroundColor = BaseColor.LIGHT_GRAY;

            tempParagraph = new Paragraph("Quantity", fontBold);
            tempParagraph.Alignment = Element.ALIGN_CENTER;
            PdfPCell quantityCell = new PdfPCell();
            quantityCell.AddElement(tempParagraph);
            quantityCell.BackgroundColor = BaseColor.LIGHT_GRAY;

            tempParagraph = new Paragraph("Unit Price", fontBold);
            tempParagraph.Alignment = Element.ALIGN_CENTER;
            PdfPCell unitPriceCell = new PdfPCell();
            unitPriceCell.AddElement(tempParagraph);
            unitPriceCell.BackgroundColor = BaseColor.LIGHT_GRAY;

            tempParagraph = new Paragraph("Location", fontBold);
            PdfPCell locationCell = new PdfPCell();
            locationCell.AddElement(tempParagraph);
            locationCell.BackgroundColor = BaseColor.LIGHT_GRAY;

            tempParagraph = new Paragraph("Sum", fontBold);
            PdfPCell sumCell = new PdfPCell();
            sumCell.AddElement(tempParagraph);
            sumCell.BackgroundColor = BaseColor.LIGHT_GRAY;

            using (MimosaProductsEntitiesEF db = new MimosaProductsEntitiesEF())
            {
                var dates = db.Sales.Select(x => x.Date).Distinct();

                double grandTotal = 0;

                foreach (var date in dates)
                {
                    tempParagraph = new Paragraph("Date: " + date, font);
                    PdfPCell dateCell = new PdfPCell();
                    dateCell.Colspan = 5;
                    dateCell.AddElement(tempParagraph);
                    dateCell.BackgroundColor = BaseColor.LIGHT_GRAY;

                    table.AddCell(dateCell);

                    table.AddCell(productCell);
                    table.AddCell(quantityCell);
                    table.AddCell(unitPriceCell);
                    table.AddCell(locationCell);
                    table.AddCell(sumCell);

                    var products =
                        from s in db.Sales
                        where s.Date == date
                        join p in db.Products on s.ProductId equals p.ProductId
                        join l in db.Locations on s.LocationId equals l.LocationId
                        select new
                            {
                                Product = p.ProductName,
                                Quantity = s.Quantity,
                                UnitPrice = s.UnitPrice,
                                LocationName = l.LocationName,
                                Sum = s.Sum
                            };

                    double totalSum = 0;

                    foreach (var product in products)
                    {
                        tempParagraph = new Paragraph(product.Product, font);
                        PdfPCell pr = new PdfPCell();
                        pr.AddElement(tempParagraph);

                        tempParagraph = new Paragraph(product.Quantity.ToString(), font);
                        tempParagraph.Alignment = Element.ALIGN_CENTER;
                        PdfPCell qu = new PdfPCell();
                        qu.AddElement(tempParagraph);

                        tempParagraph = new Paragraph(product.UnitPrice.ToString(), font);
                        tempParagraph.Alignment = Element.ALIGN_CENTER;
                        PdfPCell up = new PdfPCell();
                        up.AddElement(tempParagraph);

                        tempParagraph = new Paragraph(product.LocationName, font);
                        PdfPCell ln = new PdfPCell();
                        ln.AddElement(tempParagraph);

                        tempParagraph = new Paragraph(product.Sum.ToString(), font);
                        tempParagraph.Alignment = Element.ALIGN_RIGHT;
                        PdfPCell su = new PdfPCell();
                        su.AddElement(tempParagraph);

                        table.AddCell(pr);
                        table.AddCell(qu);
                        table.AddCell(up);
                        table.AddCell(ln);
                        table.AddCell(su);

                        totalSum += product.Sum;
                    }


                    tempParagraph = new Paragraph("Total sum for " + date + ":");
                    tempParagraph.Alignment = Element.ALIGN_RIGHT;
                    PdfPCell totalSumCell = new PdfPCell();
                    totalSumCell.Colspan = 4;
                    totalSumCell.AddElement(tempParagraph);

                    tempParagraph = new Paragraph(String.Format("{0:0.00}", totalSum), fontBold);
                    tempParagraph.Alignment = Element.ALIGN_RIGHT;
                    PdfPCell ts = new PdfPCell();
                    ts.AddElement(tempParagraph);

                    table.AddCell(totalSumCell);
                    table.AddCell(ts);

                    grandTotal += totalSum;
                }

                tempParagraph = new Paragraph("Grand Total:");
                tempParagraph.Alignment = Element.ALIGN_RIGHT;
                PdfPCell grandTotalCell = new PdfPCell();
                grandTotalCell.Colspan = 4;
                grandTotalCell.AddElement(tempParagraph);

                tempParagraph = new Paragraph(String.Format("{0:0.00}", grandTotal), fontBold);
                tempParagraph.Alignment = Element.ALIGN_RIGHT;
                PdfPCell gt = new PdfPCell();
                gt.AddElement(tempParagraph);

                table.AddCell(grandTotalCell);
                table.AddCell(gt);
            }

            doc.Add(table);
            doc.Close();
        }
    }
}
